#-*- coding:utf8 -*-
import unittest
from pgpauth.views import MyGnuPG

class MyGnuPGTestCase(unittest.TestCase):
    def setUp(self):
        self.public_key_content = '''-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.6 (GNU/Linux)

mQGiBEnn2OYRBACKCvr7c20dwnS8G1FZaY65k42S+Ix9UvA6lcOUVdMTWsBb2ypz
S+f1hKp3aGzz8kOG7ZOQyZdtsSqUCtHquNdJ9GtREiVmTL/sk0Nld5ldzQynrKli
6uUXeQBsYxpzDF4+IqL9sEnSjx5WlOy/uV42XYLE/v6QDYZi1FwGf5SsywCgtIz5
6hJAgyZA8euz8OGNBOgMQQED/1XKyytzhoD6O6m+Oxt7W1Dc1h112q5QjfYDDNOQ
LpAIn82WPJmUvJ1hfgsupi0SivVMn4qvbZN9xCtiYAjsPbJyh/QRc9sQbJs9tkD2
v1v9liog2ocEC6JFB+tpMc9nm5Xu4w0ZBzdNJP6Xp083Jblmfrh/qcS3SZ1NUxOy
Qt9IA/9OPhis3NzOBl8Yulw2qZt/K2FmiC+aaKEGT8BiVPUiknTwtykE0OKAXoM7
2fJGpedIg1LPLNAHoNPRalgRreLZ1VuEFj+s2iYdCpu2unB5zExryRpaSq9v/Fep
SqkBfqP51SsHMx9K8YR0xiP/HuL82LaRwHQPf4BZvD9s7JkGfrS1ZGphbmdvLXBn
cGF1dGggKGV4YW1wbGUgcGdwIGtleSBvZiBodHRwOi8vY29kZS5nb29nbGUuY29t
L3AvZGphbmdvLXBncGF1dGgvIGV2ZXJ5b25lIGNhbiB0YWtlcyB0aGUgcHJpdmF0
ZSBrZXksIHNvIGRvbid0IHVzZSB0aGlzIGtleSBmb3IgZW5jcnlwdC4pIDxob2Ft
b24rZGphbmdvLXBncGF1dGhAZ21haWwuY29tPohgBBMRAgAgBQJJ59jmAhsDBgsJ
CAcDAgQVAggDBBYCAwECHgECF4AACgkQ/Q6BBVD3+SwpcwCfSqq5oZrlgyIyEd3l
wKoFOPttPnsAn2X3MtuMEGd3BIU8CyR3QDDvjiIbuQENBEnn2OYQBADCr9LgHB1x
SGUQfVUTj0TpBI+0UBZDOFWinLdohaCF2W/cdUhpVgm2miVhOwwj5ASjeeh2hEns
jmm4J1W4DYZgkF4qZJcMJcNrH1sPmomleODFIngwc8fBWRq21dZofUr0CVPItIqq
eTVejpiH6PTjozK3xr7nXdgnHyw2dMD5PwADBQP/QgH/dxJwJavGfoMsmWzfX5kD
KkolxkKtnUvQw4uYEj2ImdIge7VtkAY6Nf2lhPwCRqAvQtNUGj5Qj7RLe6URIXZF
wgJOHFKE8Xoyw6TcQEFEneyt/lJFqcrCAaMBfhd4+BRKGsmOCAY/gfL9kpBoVI6Z
CofPkFAgVeYlnl11mzqISQQYEQIACQUCSefY5gIbDAAKCRD9DoEFUPf5LB3FAKCH
Yc1BXSrzezdIlB4czAwprjfktwCfTXaL9QmXvh+3nnRwKbef04l/zzw=
=DN1h
-----END PGP PUBLIC KEY BLOCK-----
'''
        self.signed_content = '''-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

django-pgpauth test
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Use GnuPG with Firefox : http://getfiregpg.org (Version: 0.7.5)

iD8DBQFJ590o/Q6BBVD3+SwRAoqvAJ9GweAuFmxzHwuvo85aFZ1OQao62QCfch3A
fqdK1QxPB3LhtZlX7nN4Qf0=
=bOMW
-----END PGP SIGNATURE-----
'''
        self.to_encrypt_content = 'django-pgpauth test\n'
        self.encrypted_content = '''-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Use GnuPG with Firefox : http://getfiregpg.org (Version: 0.7.5)

hQEOA5WHV5dj+rmTEAP/Q2dNJt91V4M1dZIWL+Kus7k7/1rynSA4tY5k+CZb2N/4
UjhF8kZjI1ySWQLeGtEjtH7LoCD766xEifxZt9cWFQhq17XHBwMMvVE2SolLcKus
us//n2HhMJbQn9U82/KE26AV6IvLsyfviR3iFI21VPuROwF/M9zm4Xc5+lbH/noE
AJrgkr6zSkLDIv4yyM5EXwVISbfe5UAui62jYGQI9eQMI3yKjPZ7Zg3EqunHjJ0U
bxjePkY1ZPWKUszOJX/2QwGL67zrI0GHgkrcDKo7yoPY0Wwq9MMYuIEQngf51RnN
Q2iwS9wSrLRlmndnzU9rii0Yls1tl/GZTEx1INSxBrJD0k8BPAdQi0VB5WszG1r8
bRdH5+RnzEw1FFAguXYv8mKYQtS9agV7Ib2OJviiy7XR7xy5UdBwWSX5m9eE4peq
pQMJEQq4lJIkKUq1DFg/1gQT
=AaSq
-----END PGP MESSAGE-----
'''
        self.public_fingerprint = '9C12BDB997D99D2D72D7637DFD0E810550F7F92C'
        self.private_key_content = '''-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: GnuPG v1.4.6 (GNU/Linux)

lQG7BEnn2OYRBACKCvr7c20dwnS8G1FZaY65k42S+Ix9UvA6lcOUVdMTWsBb2ypz
S+f1hKp3aGzz8kOG7ZOQyZdtsSqUCtHquNdJ9GtREiVmTL/sk0Nld5ldzQynrKli
6uUXeQBsYxpzDF4+IqL9sEnSjx5WlOy/uV42XYLE/v6QDYZi1FwGf5SsywCgtIz5
6hJAgyZA8euz8OGNBOgMQQED/1XKyytzhoD6O6m+Oxt7W1Dc1h112q5QjfYDDNOQ
LpAIn82WPJmUvJ1hfgsupi0SivVMn4qvbZN9xCtiYAjsPbJyh/QRc9sQbJs9tkD2
v1v9liog2ocEC6JFB+tpMc9nm5Xu4w0ZBzdNJP6Xp083Jblmfrh/qcS3SZ1NUxOy
Qt9IA/9OPhis3NzOBl8Yulw2qZt/K2FmiC+aaKEGT8BiVPUiknTwtykE0OKAXoM7
2fJGpedIg1LPLNAHoNPRalgRreLZ1VuEFj+s2iYdCpu2unB5zExryRpaSq9v/Fep
SqkBfqP51SsHMx9K8YR0xiP/HuL82LaRwHQPf4BZvD9s7JkGfgAAn1oYSWMFLtl4
gtp0MWsrHbmbdaPaCTu0tWRqYW5nby1wZ3BhdXRoIChleGFtcGxlIHBncCBrZXkg
b2YgaHR0cDovL2NvZGUuZ29vZ2xlLmNvbS9wL2RqYW5nby1wZ3BhdXRoLyBldmVy
eW9uZSBjYW4gdGFrZXMgdGhlIHByaXZhdGUga2V5LCBzbyBkb24ndCB1c2UgdGhp
cyBrZXkgZm9yIGVuY3J5cHQuKSA8aG9hbW9uK2RqYW5nby1wZ3BhdXRoQGdtYWls
LmNvbT6IYAQTEQIAIAUCSefY5gIbAwYLCQgHAwIEFQIIAwQWAgMBAh4BAheAAAoJ
EP0OgQVQ9/ksKXMAn0qquaGa5YMiMhHd5cCqBTj7bT57AJ9l9zLbjBBndwSFPAsk
d0Aw744iG50BMQRJ59jmEAQAwq/S4BwdcUhlEH1VE49E6QSPtFAWQzhVopy3aIWg
hdlv3HVIaVYJtpolYTsMI+QEo3nodoRJ7I5puCdVuA2GYJBeKmSXDCXDax9bD5qJ
pXjgxSJ4MHPHwVkattXWaH1K9AlTyLSKqnk1Xo6Yh+j046Myt8a+513YJx8sNnTA
+T8AAwUD/0IB/3cScCWrxn6DLJls31+ZAypKJcZCrZ1L0MOLmBI9iJnSIHu1bZAG
OjX9pYT8AkagL0LTVBo+UI+0S3ulESF2RcICThxShPF6MsOk3EBBRJ3srf5SRanK
wgGjAX4XePgUShrJjggGP4Hy/ZKQaFSOmQqHz5BQIFXmJZ5ddZs6AAD0D5BEB9OK
L91H8BFjEiNzSJf1sLp8GTCZn4JiDbwPiQ4aiEkEGBECAAkFAknn2OYCGwwACgkQ
/Q6BBVD3+SwdxQCgh2HNQV0q83s3SJQeHMwMKa435LcAn012i/UJl74ft550cCm3
n9OJf888
=clAK
-----END PGP PRIVATE KEY BLOCK-----
'''
        self.email = 'hoamon+django-pgpauth@gmail.com'
        self.mygnupg = MyGnuPG()

    def testGetEmailFromListKey(self):
        email = self.mygnupg.getEmailFromListKey(self.public_fingerprint)
        print
        print '>>>>ListEmailFromKey: email=%s' % email
        print '____ListEmailFromKey end<<<<'
        self.assertEquals(email, self.email)

    def testImportKey(self):
        public_fingerprint = self.mygnupg.importKey(self.public_key_content)
        print
        print '>>>>ImportKey: public_fingerprint=%s' % public_fingerprint
        print '____ImportKey end<<<<'
        self.assertEquals(public_fingerprint, self.public_fingerprint)

    def testExportKey(self):
        public_key_content = self.mygnupg.exportKey(self.public_fingerprint)
        public_key_content1 = ''.join(public_key_content.split('\n')[3:])
        public_key_content2 = ''.join(self.public_key_content.split('\n')[3:])
        print
        print '>>>>ExportKey: public_key_content=%s' % public_key_content
        print '____ExportKey end<<<<'
        self.assertEquals(public_key_content1, public_key_content2)

    def testVerifySignedText(self):
        public_fingerprint = self.mygnupg.verifySignedText(self.signed_content)
        print
        print '>>>>VerifySignedText: public_fingerprint=%s' % public_fingerprint
        print '____VerifySignedText end<<<<'
        self.assertEquals(public_fingerprint, self.public_fingerprint)

    def testDecryptText(self):
        content = self.mygnupg.decryptText(self.encrypted_content)
        print
        print '>>>>DecryptText: content=%s' % content
        print '____DecryptText end<<<<'
        self.assertEquals(content, self.to_encrypt_content)

    def testEncryptText(self):
        content = self.mygnupg.decryptText(
            self.mygnupg.encryptText(self.to_encrypt_content,
            [self.public_fingerprint])
        )
        print
        print '>>>>EncryptText: content=%s' % content
        print '____EncryptText end<<<<'
        self.assertEquals(content, self.to_encrypt_content)
